package com.smt.common.utils;

import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 将请求字符串转换成列表
 *
 * @author zhangyi
 * @date 2018/10/12 15:50
 */
public class StringToListUtil<T extends Number> {
    public static final String S = ",";

    public List<T> encode(Class clazz,String str) throws ClassCastException{
        List<T> t = new ArrayList<>();
        if(!StringUtils.contains(str,S)){
            str += S;
        }
        String[] split = str.split(S);
        List<String> strings = Arrays.asList(split);
        Object ls = null;
        String name = clazz.getName();
        for(String o : strings){
            if(name.equals(Long.class.getName())){
                ls = Long.parseLong(o);
                t.add((T)ls);
            }else if(name.equals(Integer.class.getName())){
                ls = Integer.parseInt(o);
                t.add((T)ls);
            }else{
                return null;
            }
        }
        return t;
    }
}
